<?
include("includes/functions.php");

if (!empty($delete)) {
	// Delete Sites
	$sql2 = "SELECT * from oa_btb_Sites WHERE Provider_ID = '$PID'";
	$result2 = mysql_db_query($dbname,$sql2);
	
	while ($value2 = mysql_fetch_assoc($result2)) {
		$SID = $value2[Site_PID];
		$sName = $value2[SiteName];
		$c1  = $value2[SiteContact1_ID];
		$c2  = $value2[SiteContact2_ID];
		
		// Delete Contacts
		$sql3a = "SELECT * from oa_btb_Contacts WHERE Contact_PID = '$c1'";
		$result3a = mysql_db_query($dbname,$sql3a);
		$row3a = mysql_fetch_assoc($result3a);
		$cName = $row3a[GI_NameFirst]." ".$row3a[GI_NameLast];
		
		$sqlD = "DELETE from oa_btb_Contacts WHERE Contact_PID = '$c1'";
		$resultD = mysql_db_query($dbname,$sqlD);
		
		$sql3b = "SELECT * from oa_btb_Contacts WHERE Contact_PID = '$c2'";
		$result3b = mysql_db_query($dbname,$sql3b);
		$row3b = mysql_fetch_assoc($result3b);
		$cName = $row3b[GI_NameFirst]." ".$row3b[GI_NameLast];
		
		$sqlD = "DELETE from oa_btb_Contacts WHERE Contact_PID = '$c2'";
		$resultD = mysql_db_query($dbname,$sqlD);		
	}
	$sqlD = "DELETE from oa_btb_Sites WHERE Provider_ID = '$PID'";
	$resultD = mysql_db_query($dbname,$sqlD);
		
	// Delete Products
	$sql4 = "DELETE from oa_btb_Products WHERE Provider_ID = '$PID'";
	$result4 = mysql_db_query($dbname,$sql4);
	
	// Delete Populations
	//$sql5 = "DELETE from oa_btb_Populations WHERE Provider_ID = '$PID'";
	//$result5 = mysql_db_query($dbname,$sql5);
	
	// Delete Disabilities
	$sql4 = "DELETE from oa_btb_Disabilities WHERE Provider_ID = '$PID'";
	$result4 = mysql_db_query($dbname,$sql4);
	
	// Delete DisabilityText
	$sql5 = "DELETE from oa_btb_DisabilityText WHERE Provider_ID = '$PID'";
	$result5 = mysql_db_query($dbname,$sql5);
	
	$sqlD = "DELETE from oa_btb_Providers WHERE Provider_PID = '$PID'";
	$resultD = mysql_db_query($dbname,$sqlD);	
}

// Make Visible
if (!empty($makeVisible)) {
	for($a=0;$a<=$no;$a++) {
		$v = "v".$a;
		$v = $$v;
		
		$pid = "PID".$a;
		$pid = $$pid;
		
		$sql = "UPDATE oa_btb_Providers SET IsVisible = '$v' WHERE Provider_PID = '$pid'";
		$result = mysql_db_query($dbname,$sql);			
	
	}
}

// Get all service providers
include("provider_search.php");

include("includes/header.php"); ?>
<script language="JavaScript">
function confirmSubmit() {
	var agree=confirm("Are you sure you want to delete this Service Provider?");
	if (agree)
		return true ;
	else
		return false ;
}

function checkPages(pages) {
	var pageNo = document.pageForm.page_current.value;
	if (isNaN(parseFloat(pageNo)) || (pageNo < 1) || (pageNo > pages)) {
		alert("Please enter a number between 1 and "+pages);
		document.pageForm1.page_current.value = 1;
		document.pageForm1.page_current.focus();
		document.pageForm1.page_current.style.background = '#FF9999';
		return false;
	} else {
		return true;
	}
}

function checkAll() {
	for (var j = 0; j <= <? echo $limit-1; ?>; j++) {
		box1 = eval("document.main.image_" + j); 		
		if (box1.checked == false) box1.checked = true;			
   }
}

function uncheckAll() {
	for (var j = 0; j <= <? echo $limit-1; ?>; j++) {
		box1 = eval("document.main.image_" + j); 			
		if (box1.checked == true) box1.checked = false;			
   }
}

</script>
<div id="header2">
	<? include("includes/main_menu.php"); ?>
</div>
<? include("includes/sub_menu_contacts.php"); ?>
<div id="subNoLogin"></div>
<div id="pageHeader"><h1 class="headerContacts">Contact Manager : <b>View Messages Concerning Service Providers</b></h1></div>

<div id="pageInstructions"><img src="images/help.gif"> Below is a list of existing Service Provider Organizations. Use the
buttons to the left of each entry to View, Edit or Delete the service provider.<br><br>

Check the 'Visible' box to the right of each Service Provider and click SAVE to make them appear
on the website.
<p></p>
</div>

<div id="datatable">
<? include("includes/provider_search_form.php"); ?>

<form action="<?=$PHP_SELF; ?>?makeVisible=1&<?=$linkString; ?>" method="POST">
<input type="hidden" name="page" value="<?=$page; ?>">
<input type="hidden" name="start" value="<?=$start; ?>">
<input type="hidden" name="limit" value="<?=$limit; ?>">
<input type="hidden" name="search" value="<?=$search; ?>">

<table class="datatable" summary="A List of Current Service Providers">
	<caption>
	<?
	if (isset($visible)) { echo "View Invisible Service Providers Only : [<a href=\"$PHP_SELF\">View All</a>]"; }
	else { echo "View All Service Providers"; }
	?>
	</caption>
	
	<? if ($rows > 0) { ?>
	<tr>
		<th scope="col"></th>
		<th scope="col">Organization Name</th>
		<th scope="col">Phone</th>
		<th scope="col">Email</th>
		<th scope="col">Updated</th>
		<th scope="col">Visible</th>
	</tr>
	<?
	$i = 0;
	while ($value = mysql_fetch_assoc($result)) {	
		if (($i % 2) > 0) { $class = "altrow"; }
		else { $class = null; }
		
		$PID = $value[Provider_PID];
		$pName = $value[ProviderNameFull];
  		$pMainSiteID = $value[ProviderMainSite_ID];
  		$pVisible = $value[IsVisible];
  		$pUpdated = $value[ModDate];
		
		// Get service provider contact information
		$sql2 = "SELECT * from oa_btb_Sites WHERE Site_PID = '$pMainSiteID' AND IsVisible = '1'";
		$result2 = mysql_db_query($dbname,$sql2);
		$sRow = mysql_fetch_assoc($result2);
		$sName    = stripslashes($sRow[SiteName]);
		$sStreet1 = $sRow[SiteStreet1];
		$sStreet2 = $sRow[SiteStreet2];
		$sCity    = $sRow[SiteCity];
		$sState   = $sRow[SiteStateAbbr];
		$sZip     = $sRow[SiteZipCode];
		$sPhone   = $sRow[SitePhoneMain];
		$sEmail   = $sRow[SiteEmailGeneral];
		$sURL     = $value[ProviderWebURL];
	?>
	<tr class="<?=$class; ?>">
		<td class="nav"><div class="buttons"><a href="service_provider_view.php?PID=<?=$PID; ?>" class="buttonView" title="View Details"><i>View Details</i></a><a href="service_provider_edit.php?PID=<?=$PID; ?>" class="buttonEdit" title="Edit"><i>Edit</i></a><a href="<?=$PHP_SELF; ?>?delete=1&PID=<?=$PID; ?>&start=<?=$start; ?>&page=<?=$page; ?>&limit=<?=$limit; ?>&<?=$linkString; ?>" class="buttonDelete" onclick="return confirmSubmit();" title="Delete"><i>Delete</i></a></div></td>
		<td><a href="service_provider_view.php?PID=<?=$PID; ?>" title="Click to View Details for <?=$pName; ?>"><?=$pName; ?></a></td>
		<td><?=$sPhone; ?></td>
		<td><a href="mailto:<?=$sEmail; ?>" title="Send Email to <?=$pName; ?>"><?=$sEmail; ?></a></td>
		<td align="right"><? timestamp_to_date($pUpdated); ?></td>
		<td align="center">
			<input type="checkbox" name="v<?=$i; ?>" value="1" <? if($pVisible == 1) { echo "checked"; } ?>>
			<input type="hidden" name="PID<?=$i; ?>" value="<?=$PID; ?>">
		</td>
	</tr>
	<?  $i++; }
		} else { ?>
	<tr><td>
	There are no service providers entered at this time.
	</td></tr>
	<? } ?>
	
</table>
<input type="hidden" name="no" value="<? $no = $i-1; echo "$no"; ?>">
<? if ($rows > 0) { ?>
<div align="right"><br><input type="submit" value="Update Visible"></div>
<? } ?>
</form>
</div>		


<? include("includes/footer.php"); ?>
